function uploadImage(file,imgId,valId) {
	var progress = $.AMUI.progress;
	if(typeof(imgId)=="undefined")imgId="Cover";
	if(typeof(valId)=="undefined")valId="cover";
	$("#btn-upload"+imgId).addClass("am-disabled");
	if(!/image\/\w+/.test(file.type)){
    alert("文件必须为图片！");
		$("#btn-upload"+imgId).removeClass("am-disabled");
        return false;
  }
	var reader = new FileReader();
    reader.readAsDataURL(file);
    reader.onload = function(e){
			$("#img"+imgId).attr("src",this.result);
    }
	//console.dir(file);
	//progress.start();
	var fd = new FormData();
	fd.append("uploadImg", file);
	var xhr = new XMLHttpRequest();
	xhr.upload.addEventListener("progress", function(evt){
		if (evt.lengthComputable) {
      var percentComplete = Math.round(evt.loaded/ evt.total);
      //progress.set(percentComplete);
    }
	}, false);
	xhr.addEventListener("load", function(evt){
		//progress.done(true);
		//console.dir(evt.target);
		$("#btn-upload"+imgId).removeClass("am-disabled");
		var img=eval('('+evt.target.responseText+')');
		//console.log(evt.target.responseText);
		if(img.success){
			$("#data-"+valId).val(img.id);
			mAlert({title:"提示",text:"上传成功！"});
		}else{
			mAlert({title:"错误",text:img.msg});
		}
	}, false);
	xhr.addEventListener("error", function(evt){
		//progress.remove();
		$("#btn-upload"+imgId).removeClass("am-disabled");
		mAlert({title:"错误",text:"上传失败！"});
	}, false);
	xhr.addEventListener("abort", function(evt){
		//progress.remove();
		$("#btn-upload"+imgId).removeClass("am-disabled");
	}, false);
	xhr.open("POST", "/admin/uploadImg");
	xhr.send(fd);
}

function gotoUrl(url){
	window.location.href=url;
}

function deleteOne(id,type,refer){
	var path="delete";
	if(typeof(type)!="undefined"){
		path="../"+type+"/"+path;
	}
	if(typeof(refer)=="undefined"){
		refer="list";
	}
	var $Confirm = $("#my-confirm");
	var $confirmBtn = $Confirm.find('[data-am-modal-confirm]');
	$confirmBtn.one('click', function() {
		$loading.modal('open');
		$.post(path, { "id": id,"ajax":true },function(data){
			$loading.modal('close');
			if(data){
				if(data && data.success){
					location.replace(refer);
				}else{
					mAlert({
						title:"错误",
						text:data.error
					});
				}
			}else{
				mAlert({
					title:"错误",
					text:"操作失败！"
				});
			}
		}, "json");
	});

	$Confirm.find(".text").text("你确定要删除这条记录吗？");
	if($Confirm.is(".am-modal-out")){
		$Confirm.modal('open');
	}else{
		$Confirm.modal({relatedTarget: this});
	}
}

function mAlert(options){
	if(typeof(options.title)=="undefined")options.title	= "LeanCloud CMS";
	if(typeof(options.text)=="undefined")options.text	= "";
	$("#my-alert .title").html(options.title);
	$("#my-alert .text").html(options.text);

	var $Alert = $("#my-alert");
	var $confirmBtn = $Alert.find('[data-am-modal-confirm]');
	$confirmBtn.one('click', function() {
		$Alert.modal('close');
		//console.dir(options);
		if($.isFunction(options.onConfirm)){
			options.onConfirm();
		}
		//options.onConfirm();
	});
	if($Alert.is(".am-modal-out")){
		$Alert.modal('open');
	}else{
		$Alert.modal({relatedTarget: this});
	}
}
